﻿using System;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;

namespace Byteabyte.GCalICSImporter
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowMessage("GCalICSImporter v." + Assembly.GetExecutingAssembly().GetName().Version);
            ShowMessage("");
            if(args.Length != 1)
            {
                ShowError("Invalid arguments.");
                return;
            }

            var config = new Config
                             {
                                 UserName = ConfigurationManager.AppSettings["UserName"],
                                 Password = ConfigurationManager.AppSettings["Password"],
                                 CalendarUrl = ConfigurationManager.AppSettings["CalendarUrl"]
                             };


            ShowMessage("Processing '" + args[0] + "'...");

            var icsFile = new IcsFile();
            try
            {
                icsFile.Parse(args[0]);

                GCalClient.CreateEvents(icsFile, config);
                ShowMessage("Events imported to the calendar.");

                Process.Start(config.CalendarUrl);
            }
            catch(Exception ex)
            {
                ShowError("Error parsing file: " + ex.Message);
            }
        }

        static void ShowMessage(string message)
        {
            Console.WriteLine(message);
        }

        static void ShowError(string message)
        {
            ShowMessage("* " + message);
        }
    }
}
